<?php

namespace App\Models;

use App\Common\Constant\Params;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;

class HistorySystemBalance extends BaseModel
{
    protected $table = 'history_system_balance';

    const CREATED_AT = 'add_time';
    const UPDATED_AT = null;

    protected $dateFormat = 'U';

    protected $fillable = [
        'trade_id',
        'user_id',
        'user_type',
        'direction',
        'amount',
        'current_balance',
        'new_balance',
    ];

    protected $casts = [
        'amount' => 'double',
        'current_balance' => 'double',
        'new_balance' => 'double',
    ];

    public function assoc(){
        Relation::morphMap([
            Params::USER_TYPE_MERCHANT => Merchant::class,
            Params::USER_TYPE_MASTER => Master::class,
        ]);
        return $this->morphTo(null,'user_type','user_id');
    }

    /**
     * 用户类型文字
     * user_type_text
     */
    public function getUserTypeTextAttribute()
    {
        return Params::$USER_TYPE[$this->user_type];
    }

    /**
     * 收支方向文字
     * direction_text
     */
    public function getDirectionTextAttribute()
    {
        return Params::$TRADE_DIRECTION[$this->direction];
    }
}
